package com.wyj.controller;

import com.wyj.mapper.YlrcRoleMapper;
import com.wyj.pojo.YlrcUser;
import com.wyj.service.impl.YlrcRoleServiceImpl;
import com.wyj.service.impl.YlrcUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ：wyj
 * @description：TODO
 * @date ：2022/11/19 7:05 下午
 */
@RestController
@RequestMapping("/roleAndUser")
public class RoleAndUserController {

    @Autowired
    YlrcRoleServiceImpl ylrcRoleServiceImpl;

    @Autowired
    YlrcUserServiceImpl ylrcUserServiceImpl;

    //查询角色下的用户
    @GetMapping("{/username}")
    public List<Map<String,Object>> getRoleAndUsers(@PathVariable("username") String username) {
        return ylrcRoleServiceImpl.getRoleAndUsers(username);
    }

    //添加角色下的用户
    @PostMapping
    public String addRoleAndUsers(@ModelAttribute("ylrcUser") YlrcUser ylrcUser) {
        try {
            int i = ylrcUserServiceImpl.addUser(ylrcUser);
            if (i > 0) return "添加一个角色下的用户成功";
        } catch (Exception e) {
            new RuntimeException(e);
        }
        return "添加一个角色下的用户失败";
    }

    //移除角色下的用户 ()
    //移除一个用户拥有的角色
    @DeleteMapping
    public String deleteUserAndRoles(String username,int roleId) {
        try{
            int i = ylrcUserServiceImpl.deleteUserAndRole(username, roleId);
            if (i >=0 ) return "移除一个用户拥有的角色成功";
        } catch (Exception e) {
            new RuntimeException(e);
        }

        return "移除一个用户拥有的角色失败";
    }


}
